-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathaddition in LinkedList.py
94 lines (77 loc) · 2.13 KB
/
addition in LinkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Contribution By: Shantanu Gupta
# Github: Shantanugupta1118
# DAY 9 of DAY 100
# LeetCode - 02 - Add Two Numbers
# https://leetcode.com/problems/add-two-numbers/
# Solution
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LL:
def __init__(self):
self.head = None
def add(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def display(self):
curr = self.head
if curr == None:
print("None")
else:
while curr:
print(curr.data, end = ' -> ')
curr = curr.next
print("NULL", end='\n')
def solve(self, l1, l2):
curr1 = l1.head
curr2 = l2.head
carry = 0
while curr1 is not None:
s = str(curr1.data + curr2.data)
temp = '0'
if len(s) == 2:
temp = s[0]
s = s.replace(s[0], '')
print(temp, s)
self.add(int(s)+int(carry))
carry = int(temp)
curr1 = curr1.next
curr2 = curr2.next
arr1 = [2,4,3]
arr2 = [5,6,4]
l1 = LL() #LinkedList 1
for i in arr1:
l1.add(i)
l2 = LL() #LinkedList 2
for i in arr2:
l2.add(i)
l3 = LL()
l3.solve(l1, l2)
l3.display()
'''
# ________________________________________________________
# LeetCode solution style:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode, c=0) -> ListNode:
val = l1.val + l2.val + c
c = val // 10
ret = ListNode(val % 10 )
if (l1.next != None or l2.next != None or c != 0):
if l1.next == None:
l1.next = ListNode(0)
if l2.next == None:
l2.next = ListNode(0)
ret.next = self.addTwoNumbers(l1.next,l2.next,c)
return ret
'''